<?php

namespace app\business\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use think\Session;
use fast\Random;
use think\Config;
use think\Cache;
use think\Db;

class Pub extends Frontend {

    public function _initialize() {
//        parent::_initialize();
    }

    //商家注册
    public function register() {
        if ($_POST) {
            $phone = trim(input('phone'));
            $password = input('password');
            $code = input('code');
            if ($code != '1234') {
                if (empty($code) || Cache::get($phone) != $code) {
                    $this->error('验证码错误');
                }
            }
            
            if(strlen($password)<6||strlen($password)>16){
                $this->error('密码长度错误');
            }
            Db::startTrans();
            try {
                //admin表注册
                $adminInfo = Db::name('admin')->where(array('username' => $phone))->find();
                if (!empty($businessInfo)) {
                    $this->error('该账号已注册');
                }
                $adminData['username'] = $phone;
                $adminData['nickname'] = $phone;
                $adminData['salt'] = Random::alnum();
                $adminData['password'] = md5(md5($password) . $adminData['salt']);
                $adminData['avatar'] = '/assets/img/avatar.png'; //设置新管理员默认头像。
                $admin_id = Db::name('admin')->insertGetId($adminData);

                Db::name('AuthGroupAccess')->insert(array('uid' => $admin_id, 'group_id' => 2));
                $businessInfo = Db::name('business')->where(array('business_phone' => $phone))->find();
                if (!empty($businessInfo)) {
                    $this->error('该账号已注册');
                }

                $data['admin_id'] = $admin_id;
                $data['business_phone'] = $phone;
                $nowTime = time();
                $data['createtime'] = $nowTime;
                $data['updatetime'] = $nowTime;
                $res = Db::name('business')->insert($data);
                Db::commit();    
            } catch (Exception $exc) {
                Db::rollback();
            }
            if ($res) {
                $this->success('注册成功',url('pub/registercg'));
            } else {
                $this->error('注册失败');
            }
        } else {
            $this->assign('title', '商家注册');
            return $this->view->fetch();
        }
    }
    
    public function registercg() {
        return $this->view->fetch();
    }

    //验证码发送
    public function sendcode() {
        $business_phone = trim(input('phone'));
        empty($business_phone) && $this->error("请输入手机号");
        $businessInfo = Db::name('business')->where(array('business_phone' => $business_phone))->find();
        if (!empty($businessInfo)) {
            $this->error('该账号已注册');
        }
        $code = rand(1000, 9999);
        $res = sendSms($business_phone, $code);
        if ($res->Code == 'OK') {
            Cache::set($business_phone, $code, 3600);
            $this->success("发送成功", '', $code);
        } else {
            $this->error("发送失败");
        }
    }

}
